package ThreadDemo.Ticket;

public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object obj = new Object();
    static {
        System.out.println("100张票待售");
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           synchronized (obj){
               if (tickets == 0) {
                   break;
               } else {
                   tickets--;
                   System.out.println(Thread.currentThread().getName()+"卖出一张票，还有" + tickets + "张票");
               }
           }
        }
    }
}
